import java.util.Arrays;

public class arrayCopy {
    // 一维数组复制
    public static int[] copy1D(int[] arr) {
        if (arr == null) {
            return null;
        }
        int[] dest = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            dest[i] = arr[i];
        }
        return dest;
    }

    // 二维数组复制
    public static int[][] copy2D(int[][] arr) {
        if (arr == null) {
            return null;
        }
        int[][] dest = new int[arr.length][];
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != null) {
                dest[i] = new int[arr[i].length];
                for (int j = 0; j < arr[i].length; j++) {
                    dest[i][j] = arr[i][j];
                }
            }
        }
        return dest;
    }

    public static void main(String[] args) {
        // 一维数组
        int[] a1 = {10, 20, 30};
        int[] b1 = copy1D(a1);
        System.out.println("一维原数组: " + Arrays.toString(a1));
        System.out.println("一维复制后: " + Arrays.toString(b1));
        System.out.println("是否不是同一个数组(同一地址)/是否成功复制: "+(a1 != b1));

        // 二维数组
        int[][] a2 = {
                {1, 2},
                {3, 4, 5}
        };
        int[][] b2 = copy2D(a2);
        System.out.println("\n二维原数组:");
        for (int[] row : a2) {
            System.out.println(Arrays.toString(row));
        }
        System.out.println("二维复制后:");
        for (int[] row : b2) {
            System.out.println(Arrays.toString(row));
        }
        System.out.println("是否不是同一个数组(同一地址)/是否成功复制: "+(a2 != b2));
    }
}
